// components/index.js
Component({
  /**
   * 指定所有 _ 开头的数据字段为纯数据字段
   */
  options: {
    pureDataPattern: /^_/
  },
  /**
   * 组件的属性列表
   */
  properties: {
    'config': {
      type: Object,
      observer: function(newVal, oldVal){
        if(!newVal || !newVal.startDate || !newVal.endDate || !newVal.selectedDates){
          return
        }
        this._initDate(newVal.startDate,newVal.endDate,newVal.selectedDates)
      }
    },
    'color':{
      type: String,
      observer: function(newVal, oldVal){
        if(!newVal){
          this.setData({
            color: "#EE0A24"
          })
          return
        }
        this.setData({
          color: newVal
        })
      }
    },
    'title':{
      type: String,
      observer: function(newVal, oldVal){
        this.setData({
          title: newVal
        })
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    
  },

  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached: function () { 
    },
    moved: function () {
    },
    detached: function () { 
    },
  },

  /**
   * 组件的方法列表
   */
  methods: {
    _initDate(startDateStr,endDateStr,selectList){
      
      let selectMap = new Map()
      for(let i=0;i<selectList.length;i++){
        const item = selectList[i]
        selectMap.set(item.date,item)
      }

      // yyyy-MM-dd => yyyy/MM/dd 兼容IOS日期
      startDateStr = startDateStr.replace(/\-/g, "/")
      endDateStr = endDateStr.replace(/\-/g, "/")

      const startDate = new Date(startDateStr)
      const endDate = new Date(endDateStr)

      let map = new Map()
      let currentDate = startDate
      while(currentDate <= endDate){
        // 获取月份
        const month = _dateFormat(currentDate,"yyyy-MM")
        if(!map.get(month)){
          map.set(month,[])
        }
        let list = map.get(month)

        const date = _dateFormat(currentDate,"yyyy-MM-dd")

        let item = {
          date: date,
          isSelect: selectMap.get(date)?true:false,
          day: currentDate.getDate(),
          ...selectMap.get(date)
        }

        list.push(item)

        // 增加天数
        currentDate = _dateAddDay(currentDate,1)
      }

      let allDate = []

      map.forEach(function(value,key){
        const week = _dateWeek(value[0].date)
        let item = {
          key: key,
          value: value,
          offset: week
        }
        allDate.push(item)
      });

      this.setData({
        allDate: allDate
      })
    },
    onClick(e){
      this.triggerEvent('click',e.currentTarget.dataset.item)
    }
  }

})

/**
 * 获取星期数
 */
const _dateWeek = (dateStr) => {
  // yyyy-MM-dd => yyyy/MM/dd 兼容IOS日期
  dateStr = dateStr.replace(/\-/g, "/")
  const date = new Date(dateStr)
  return date.getDay()
}


/**
 * Date 增加天数
 */
const _dateAddDay = (date,add) =>{
  date.setDate(date.getDate() + add)
  return date
}

/**
 * Date 转 String
 */
const _dateFormat = (date,format) =>{
  const year = date.getFullYear()
  const month = date.getMonth() + 1

  if(format == 'yyyy-MM'){
    return [year, month].map(_formatNumber).join('-')
  }

  const day = date.getDate()
  if(format == 'yyyy-MM-dd'){
    return [year, month, day].map(_formatNumber).join('-')
  }

  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  return [year, month, day].map(_formatNumber).join('-') + ' ' + [hour, minute, second].map(_formatNumber).join(':')
}

/**
 * 补0
 */
const _formatNumber = (n) => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
